package dk.itu.KrakMap;

import java.util.*;

/**
 * Represent a route plan, by one or more Routes
 *
 * @author ITU-KF04-2011, Group 11
 */
public class RoutePlan {

    private final List<Route> routes;

    /**
     * create a Routeplan from a list of routes.
     * @param routes takes a list of route elements.
     */
    public RoutePlan(List<Route> routes) {
        this.routes = routes;
    }

    /**
     * get all the roads in the routeplan.
     * @Iterates through each road of each route in routes and return an ArrayList of all the roads.
     */
    public ArrayList<Road> getRoads() {
        ArrayList<Road> result = new ArrayList<Road>();
        for (Route r : routes) {

            for (Road ro : r.getRoads()) {
                result.add(ro);
            }
        }
        return result;
    }

    /**
     * get the length of the routes.
     * @return the accumulated lentgh of each route en routes.
     */
    public double getLength() {
        double result = 0.0;
        for (Route r : routes) {
            result += r.getLength();
        }
        return result;
    }

    /**
     * Get the length of the route, formatted as a string
     * @return A formatted string with the length of the route
     */
    public String getFormattedlength() {
        double length = Math.round(getLength());
        if (length >= 1000.0) {
            length = length / 1000;
            return "Ruten er: " + length + " km";
        } else {
            return "Ruten er: " + length + " meter";
        }
    }

    /**
     * Get the number of routes in the routeplan
     * @return the number of routes in routes.
     */
    public int getNumOfRoutes() {
        return routes.size();
    }

    /**
     * Get the first found route that contains the given node.
     * NOTE THAT: The method returns null if the node is not in the RoutePlan.
     * @param n The node to search for the route
     * @return The first found route
     */
    public Route getRoute(KrakNode n) {
        for (Route r : routes) {
            if (r.goesThroughNode(n)) {
                return r;
            }
        }
        return null;
    }

    /**
     * check for each route if a route has an edge that is close enough to a given node
     * @param n KrakNode, the given node.
     * @param threshold the minimum distance
     * @return a route if it has an edge that is close enough to the node.
     */
    public Route getNearRoute(KrakNode n, double threshold) {
        for (Route r : routes) {
            if (r.goesNearNode(n, threshold)) {
                return r;
            }
        }
        return null;
    }
}
